//
//  WXTabBar.swift
//  weibo
//
//  Created by 王旭 on 16/4/16.
//  Copyright © 2016年 王旭. All rights reserved.
//

import UIKit

class WXTabBar: UITabBar {
    
    
    var addButtonClosure:(()->())?
    
    
    //按钮点击方法
    func addButtonClick() {
        addButtonClosure!()
    }
    
    //重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(addBtutton)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //布局按钮的位置
    override func layoutSubviews() {
        super.layoutSubviews()
        addBtutton.center = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2)
        
        //修改系统的TabBarbutton的大小和位置
         //计算更改后的宽度
        let tabBarButtonWidth = self.bounds.size.width / 5
         //定义一个按钮计数属性
        var tabBarButtonIndex = 0
         //遍历子控件
        for tabBarButton in subviews {
            //取出TabBarbutton
            if tabBarButton.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                //修改宽度
                tabBarButton.bounds.size.width = tabBarButtonWidth
                //修改位置
                tabBarButton.frame.origin.x = tabBarButtonWidth * CGFloat(tabBarButtonIndex)
                //按钮计数属性逻辑
                tabBarButtonIndex++
                if tabBarButtonIndex == 2 {
                    tabBarButtonIndex++
                }
            }

        }
    }
    
    //xib加载才会调用
    //    override func awakeFromNib() {
    //        super.awakeFromNib()
    //        addBtutton.center = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2)
    //    }
    
    

    //懒加载按钮控件
    private lazy var addBtutton :UIButton = { ()->UIButton in
        let button = UIButton()
        
        //设置按钮的点击事件
        button.addTarget(self , action: "addButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        //设置不同状态下的图片
         //普通
        button.setImage(UIImage(named:"tabbar_compose_icon_add"), forState:.Normal)
         //高亮
        button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //设置不同状态下的背景图片
         //普通
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
         //高亮
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        //调整按钮尺寸
        button.sizeToFit()
        
        return button
    }()
}
